package com.working.libbase.dialog

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import androidx.databinding.DataBindingUtil
import com.working.libbase.R
import com.working.libbase.base.BaseDialog
import com.working.libbase.databinding.DialogCommonBinding
import com.working.libcommon.exts.onClick

internal class CommonDialog(context: Context) : BaseDialog(context) {
    var mTitle: String? = null
    var mMessage: String? = null
    var mHint: String? = null
    var mConfirmText: String? = null
    var mCancelText: String? = null
    var mOnConfirm: (() -> Unit)? = null
    var mOnCancel: (() -> Unit)? = null
    private lateinit var mBinding: DialogCommonBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        mBinding = DataBindingUtil.inflate(
            LayoutInflater.from(context),
            R.layout.dialog_common, null, false
        )
        setContentView(mBinding.root)

        super.onCreate(savedInstanceState)
        mBinding.apply {
            mHint?.let {
                tvHint.text = it
            }
            mTitle?.let {
                tvTitle.text = it
            }
            mMessage?.let {
                tvMessage.text = it
            }
            if (mConfirmText.isNullOrEmpty()) {
                tvConfirm.visibility = View.GONE
            } else {
                tvConfirm.text = mConfirmText
            }
            if (mCancelText.isNullOrEmpty()) {
                tvCancel.visibility = View.GONE
            } else {
                tvCancel.text = mCancelText
            }
            tvCancel.onClick {
                dismiss()
                mOnCancel?.invoke()
            }
            tvConfirm.onClick {
                dismiss()
                mOnConfirm?.invoke()
            }
        }
    }
}